package com.uni.db;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.uni.domain.UserDetailsImpl;

@Transactional
public class UserDetailsServiceImpl implements UserDetailsService {
	
	private GenericDao dao;
	
	public void setDao(GenericDao dao) {
		this.dao = dao;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
		List<UserDetailsImpl> list = dao.getSession().createCriteria(UserDetailsImpl.class).add(Restrictions.eq("name", userName)).list();
		if(list.isEmpty()){
			throw new UsernameNotFoundException("TEST");
		}
		return list.get(0);
	}


}
